package com.tech.distribute_lock;

import java.util.UUID;
import java.util.concurrent.locks.LockSupport;

/**
 * @author litianlin
 * @date 2023/12/7 15:15
 * @description:
 */
public interface DistributeLockStategy {

    ThreadLocal<String> THREAD_UUID = ThreadLocal
            .withInitial(()->UUID.randomUUID()+"_"+Thread.currentThread().getName());

    /**
     * 加分布式锁
     * @param lockKey
     * @return 是否加锁成功
     */
    boolean tryLock(String lockKey, int expireTime);

    default String getOwnerId() {
        return THREAD_UUID.get();
    }

    /**
     * 加分布式锁
     * @param millis 等待时间
     */
    default void lockWait(int millis){
        LockSupport.parkNanos(millis*1000*1000);
    }

    /**
     * 释放分布式锁
     * @param lockKey 锁
     * @return 是否释放成功
     */
    boolean unLock(String lockKey);
}
